/*
 * @lc app=leetcode.cn id=463 lang=javascript
 *
 * [463] 岛屿的周长
 */

// @lc code=start
/**
 * @param {number[][]} grid
 * @return {number}
 */
var islandPerimeter = function(grid) {
    let result = 0
    let reGrid = grid[0].map(e => []) // 通过行，获取列
    grid.forEach(row => {
        let pre = -1
        row.forEach((item, index) => {
            if (pre === 1 && item === 1) {
                result += 2
            } else if (item === 1) {
                result += 4
            }
            reGrid[index].push(item)
            pre = item
        })
    })
    reGrid.forEach(col => {
        let pre = -1
        col.forEach((item, index) => {
            if (pre === 1 && item === 1) {
                result -= 2
            }
            pre = item
        })
    })
    return result
};
// @lc code=end

